<?php
/** 
 * 验证码检查 
 */  

//缓存
function check_session(){
   		$username=session('username');
		$upassword=session('upassword');
		/*print_r($username);
		print_r($upassword);exit('###');*/
		$time=time()-session('time');
		if(empty($username)||empty($upassword)){			
				return '1';
		}
		if($time>=1800){
			session(null);	
			return '1';
		}	
	}  


//获取access_token
function access_token(){	
		$APPID=C('APPID');
		$APPSECRET=C('APPSECRET');
		$token=M('token');
		$require=$token->find();
		$id=$require['id'];
		$access_token=$require['token'];
		$ctime=$require['ctime'];
		$time=time();
		$newtime=$ctime+7000;	
		if(empty($access_token)){
		 
			$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$APPID. "&secret=" .$APPSECRET;
			$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
			//print_r($res);exit('123');
			$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
			$access_token = $result['access_token']; 
			//print_r($access_token);exit('111');
			$data=array('token'=>$access_token,'ctime'=>$time,);
			$re=$token->add($data);	
			if(!$re){
				echo "参数错误1";		
			}
		
		}
		if(!empty($ctime) && $time>=$newtime){				
			$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .$APPID. "&secret=" .$APPSECRET;
			$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
			//echo $res;
			$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
			$access_token = $result['access_token'];
			$data=array('token'=>$access_token,'ctime'=>$time,);
			$re=$token->where('id='.$id)->save($data);		
			if(!$re){
				echo "参数错误2";	
				}		
			}
			return $access_token;
	}

//获取用户列表
function user_list(){//exit('###');
	set_time_limit(0);
	$access_token=access_token();
	//print_r($access_token);exit('@@@');
	$next_openid='';
	$url='https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token.'&next_openid='.$next_openid.'';
	$res = file_get_contents($url); 	
	$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
	//print_r($result);exit;
	$openid_list=$result['data']['openid'];
	//print_r($openid_list);exit;
	foreach ($openid_list as $key=>$value) {
		$openid=$value;
		$where['openid']=$openid;
		$res_select=M('user')->where($where)->find();
		if (empty($res_select)) {
		$info_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
		$info_res = file_get_contents($info_url); //获取文件内容或获取网络请求的内容
		$info_result = json_decode($info_res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
		//查询结果，如果有则不进行插入
		
		//添加数据
		$data = array('subscribe' =>$info_result['subscribe'] ,
					  'openid' =>$info_result['openid'] ,
					  'nickname' =>$info_result['nickname'] ,
					  'sex' => $info_result['sex'],
					  'language' =>$info_result['language'] ,
					  'city' =>$info_result['city'] ,
					  'province' =>$info_result['province'] ,
					  'country' =>$info_result['country'] ,
					  'headimgurl' =>$info_result['headimgurl'] ,
					  'subscribe_time' =>date('Y-m-d H:i:s',$info_result['subscribe_time']),
					  'remark' =>$info_result['remark'] ,
					  'groupid' =>$info_result['groupid'] ,
					  'tagid_list' =>$info_result['tagid_list'] ,
					 
					  'ctime'=>time(),
						 );
		$res_add=M('user')->data($data)->add();
	 }
	}

}





function refresh_user(){
	set_time_limit(0);
	$access_token=access_token();
	$openid_last=M('user')->order('ctime desc')->find();
	$next_openid=$openid_last;
	$url='https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token.'&next_openid='.$next_openid.'';
	$res = file_get_contents($url); 	
	$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
	$openid_list=$result['data']['openid'];
	foreach ($openid_list as $key=>$value) {
		$openid=$value;
		$where['openid']=$openid;
		$res_select=M('user')->where($where)->find();
		if (empty($res_select)) {
		$info_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
		$info_res = file_get_contents($info_url); //获取文件内容或获取网络请求的内容
		$info_result = json_decode($info_res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
		//查询结果，如果有则不进行插入
		
		//添加数据
		$data = array('subscribe' =>$info_result['subscribe'] ,
					  'openid' =>$info_result['openid'] ,
					  'nickname' =>$info_result['nickname'] ,
					  'sex' => $info_result['sex'],
					  'language' =>$info_result['language'] ,
					  'city' =>$info_result['city'] ,
					  'province' =>$info_result['province'] ,
					  'country' =>$info_result['country'] ,
					  'headimgurl' =>$info_result['headimgurl'] ,
					  'subscribe_time' =>date('Y-m-d H:i:s',$info_result['subscribe_time']),
					  'remark' =>$info_result['remark'] ,
					  'groupid' =>$info_result['groupid'] ,
					  'tagid_list' =>$info_result['tagid_list'] ,
					  'name' => $info_result['nickname'],
					  'ctime'=>time(),
						 );
		$res_add=M('user')->data($data)->add();
	 }
	}
	

}
//curl
/*function curl($url,$postdata)
    {	//print_r($postdata);exit;
        $ch = curl_init();//初始化
        curl_setopt($ch, CURLOPT_URL, $url);//设置URl地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//将curl_exec()获取的信息以文件流的形式返回，而不是直接输出。	 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_POST, 1);//开启post
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);//post数据
        $resp = curl_exec($ch);
        //print_r($resp);exit('@#');
        $error = curl_error($ch);
        if (!empty($error)) {
          return 'false';
        }

        curl_close($ch);
        return $resp;
    }*/
//用curl 获取用户列表
/*function user_list(){
	set_time_limit(0);
	$access_token=access_token();
	$next_openid='';
	$url='https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$access_token.'&next_openid='.$next_openid.'';
	$res = file_get_contents($url); 	
	$result = json_decode($res, true); //接受一个 JSON 格式的字符串并且把它转换为 PHP 变量
	//print_r($result);exit;
	$openid_list=$result['data']['openid'];
	$i=0;
	foreach ($openid_list as $key=>$value) {
		$i++;
		$openid=$value;
		$where['openid']=$openid;
		//$res_select=M('user')->where($where)->find();
		//if (empty($res_select)) {
		$user_list[]=array(
			'openid'=>$openid, 
			"lang"=> "zh-CN",
			);
	
	 if ($i==100) {
	 	$url='https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token='.$access_token.'';
	 	///print_r($url);
	 	$postdata=array('user_list'=>$user_list);
	 	//print_r($postdata);exit;
	 	$c=curl($url,json_encode($postdata));
	 	$info_result=json_decode($c);
	 	print_r($info_result);exit;
	 	$data = array('subscribe' =>$info_result['subscribe'] ,
					  'openid' =>$info_result['openid'] ,
					  'nickname' =>$info_result['nickname'] ,
					  'sex' => $info_result['sex'],
					  'language' =>$info_result['language'] ,
					  'city' =>$info_result['city'] ,
					  'province' =>$info_result['province'] ,
					  'country' =>$info_result['country'] ,
					  'headimgurl' =>$info_result['headimgurl'] ,
					  'subscribe_time' =>date('Y-m-d H:i:s',$info_result['subscribe_time']),
					  'remark' =>$info_result['remark'] ,
					  'groupid' =>$info_result['groupid'] ,
					  'tagid_list' =>$info_result['tagid_list'] ,
					  'name' => $info_result['nickname'],
					  'ctime'=>time(),
						 );
		$res_add=M('user')->data($data)->add();
	 } 
	}
//}
//exit('####');
}*/
?>